package com.hejjon.controller;

import org.quartz.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by caoshi at 21:12 2021-08-15
 */
@DisallowConcurrentExecution
//@PersistJobDataAfterExecution
public class HelloQuartz implements Job {


    private String name;

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public void execute(JobExecutionContext context) {
        String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        System.out.println("当前时间" + dateStr);
        JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
        int detailCount = jobDataMap.getInt("detailCount");
        jobDataMap.put("detailCount", detailCount + 1);
        System.out.println("detailCount " + detailCount);

//        String detail = jobDataMap.getString("detailKey");
//        System.out.println(detail);
        JobDataMap triggerDataMap = context.getTrigger().getJobDataMap();
        int triggerCount = triggerDataMap.getInt("triggerCount");
        triggerDataMap.put("triggerCount", triggerCount + 1);
        System.out.println("triggerCount " + triggerDataMap.getInt("triggerCount"));
//        String triggerValue = triggerDataMap.getString("trigger");
//        System.out.println(triggerValue);
//
//        System.out.println("name = " + name);
        // 每一次执行任务都是新的 JobDetail
        JobDetail jobDetail = context.getJobDetail();
        System.out.println("jobDetail :" + System.identityHashCode(jobDetail));
        System.out.println("job: " + System.identityHashCode(context.getJobInstance()));

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
